/******************************************************************************
 * Copyright (C) 2025 Engineer Liu Si
 * 
 * All Rights Reserved.
 * 
 * @file bsp_led.c
 * 
 * @par dependencies 
 * - bsp_led.h
 * 
 * @author liusi
 * 
 * @brief Provide the HAL APIs of Led and corresponding opetions.
 * 
 * Processing flow:
 * 
 * call directly.
 * 
 * @version V1.0 2025-09-27
 *
 * @note 1 tab == 4 spaces!
 * 
 *****************************************************************************/
#include "bsp_led.h"
#include "bsp_key_irq.h"
//******************** Thread_Func ***********************//
osThreadId_t led_TaskHandle;
const osThreadAttr_t led_Task_attributes = {
  .name = "led_Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal1,
};

//******************** Thread_Func ***********************//

//******************* Queue_Handler **********************//
QueueHandle_t led_queue;

//******************* Queue_Handler **********************//

//******************* Globle_Value **********************//
static uint16_t g_blink_times = 0;
static uint16_t g_blink_order = 0;

//******************* Globle_Value **********************//

/**
 * @brief Instantiates the bsp_led_handler_t target.
 * 
 * Steps:
 *  1.doing the specific operations.
 *  
 * 
 * @param[in] void
 * @return led_handler_status_t : Status of the function.
 * 
 * */
led_status_t led_on_off( led_operation_t led_operation )
{
		led_status_t ret = LED_OK;
			
		if( LED_TOGGLE == led_operation )
		{
			  //1.Make the LED TOGGLE
				g_blink_times = 1;
			  g_blink_order = 0;
		}
		if( LED_TEN_TOGGLE == led_operation )
		{
			  //2.Make the LED TOGGLE
				g_blink_times = 5;
			  g_blink_order = 0;
		}
		return ret;
}

/**
 * @brief Key_Task_Func for thread to run.
 * 
 * Steps:
 *  1.doing the specific operations.
 *  
 * 
 * @param[in] void
 * @return led_handler_status_t : Status of the function.
 * 
 * */
void Led_Task_Func(void *argument)
{
	key_press_status_t led_opera     =     KEY_NOT_PRESSED;

    for(;;)
 	{
		if( xQueueReceive(  key_queue, 
			                &led_opera, 
			                portMAX_DELAY )
		  )
		{
			printf("led receive is %d \r\n",led_opera);
			if( KEY_PRESSED == led_opera )
			{
				led_on_off( LED_TOGGLE );
			}
			else if( KEY_LONG_PRESSED == led_opera )
			{
				led_on_off( LED_TEN_TOGGLE );
			}
			else
			{
				led_on_off( LED_OFF );
			}
		}
    }
}

/**
 * @brief LED_OP_Function for to run.
 * 
 * Steps:
 *  1.doing the specific operations.
 *  
 * 
 * @param[in] void
 * @return led_status_t : Status of the function.
 * 
 * */
led_status_t LED_OP_Function(void)
{
	 if( g_blink_times > 0 )
	 {
		 if( g_blink_order % 2 == 0 )
		 {
			  HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
		 }
		 else
		 {
				HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
			  g_blink_times--;
		 }
		 g_blink_order++;
	 }
	 else
	 {
		 g_blink_order = 0;
	 }
	 return LED_OK;
}

